﻿
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using System.Text;
using System.Xml;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.admin.message
{
    public partial class groupMail : BaseAdmin
    {


        // Methods
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string pathAndQuery = HttpContext.Current.Request.Url.PathAndQuery;
            try
            {
                SendMailMessage message = new SendMailMessage();
                Array array = base.Request.Form["addUser"].Split(new char[] { ',' });
                message.subject = this.txtSobject.Value;
                message.body = this.fckBody.Value;
                message.html = true;
                DataTable allEnabledMail = BaseFactory.theMail.GetAllEnabledMail();
                Random random = new Random();
                for (int i = 0; i < array.Length; i++)
                {
                    message.mailTo = array.GetValue(i).ToString();
                    int num2 = random.Next(allEnabledMail.Rows.Count - 1);
                    DataRow row = allEnabledMail.Rows[num2];
                    message.from = row["fromAddress"].ToString();
                    message.fromName = row["fromName"].ToString();
                    message.mailDomain = row["mailDomain"].ToString();
                    message.mailDomainPort = Convert.ToInt32(row["mailDomainPort"]);
                    message.mailServerUserName = row["mailServerUserName"].ToString();
                    message.mailServerPassWord = row["mailServerPassWord"].ToString();
                    message.SendMailEvent += new SendMailMessage.SendMailHandler(this.SendMain);
                    new Thread(new ThreadStart(message.Send)).Start();
                }
                Thread.Sleep(100);
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送成功！\");location.href=\"messageMailList.aspx\";</script>");
            }
            catch
            {
                base.ClientScript.RegisterStartupScript(base.GetType(), "message", "<script language=\"javascript\" defer>alert(\"发送失败！\");</script>");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //if ((HttpContext.Current.Session["admin_user"] == null) && (HttpContext.Current.Session["language_version"] == null))
            //{
            //    base.ClientScript.RegisterStartupScript(base.GetType(), "sJs", "top.location=\"login.html\";", true);
            //}
        }

        protected void SendMain()
        {
        }

        protected StringBuilder UserBind()
        {
            StringBuilder builder = new StringBuilder();
            try
            {
                DataTable allUserEmail = BaseFactory.theUser.GetAllUserEmail();
                foreach (DataRow row in allUserEmail.Rows)
                {
                    builder.AppendLine("<option value=\"" + row["email"].ToString() + "\">" + row["userName"].ToString() + "</option>");
                }
            }
            catch
            {
            }
            return builder;
        }

    }
}